package com.ktg.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktg.ktgcommon.filter.TokenVerifyFilter;
import com.ktg.ktgcommon.domain.mainData.MdUnitMeasure;
import com.ktg.mapper.UnitMeasureMapper;
import com.ktg.service.UnitMeasureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class UnitMeasureServiceImpl implements UnitMeasureService {

    @Autowired
    private UnitMeasureMapper unitMeasureMapper;
    @Override
    public Page<MdUnitMeasure> checkListUnitPage(Page<MdUnitMeasure> page, String measureCode, String measureName) {
        Page<MdUnitMeasure> mdUnitMeasures = unitMeasureMapper.checkListUnitPage(page, measureCode, measureName);
        return mdUnitMeasures;
    }

    @Override
    public MdUnitMeasure CheckUnitById(Integer measureId) {
        MdUnitMeasure mdUnitMeasure = unitMeasureMapper.CheckUnitById(measureId);
        return mdUnitMeasure;
    }

    @Override
    public Integer AddOrChUnit(MdUnitMeasure mdUnitMeasure) {
        //解析jwt获取user信息
        TokenVerifyFilter tokenVerifyFilter=new TokenVerifyFilter();
        if (mdUnitMeasure.getMeasureId()!=null) {
            //设置更新时间
            mdUnitMeasure.setUpdateTime(new Date());
            //设置更新者名称
            mdUnitMeasure.setUpdateBy(tokenVerifyFilter.getUserName().getUsername());
            return unitMeasureMapper.changeUnit(mdUnitMeasure);
        }else {
            //设置创建时间
            mdUnitMeasure.setCreateTime(new Date());
            //设置创建者名称
            mdUnitMeasure.setCreateBy(tokenVerifyFilter.getUserName().getUsername());
            return unitMeasureMapper.addUnit(mdUnitMeasure);
        }
    }

    @Override
    public Integer delUnit(Integer measureId) {
        Integer integer = unitMeasureMapper.delUnit(measureId);
        return integer;
    }

    @Override
    public Integer delUnitArray(List<Integer> arrayMeasureId) {
        Integer integer = unitMeasureMapper.delUnitArray(arrayMeasureId);
        return integer;
    }
}
